
var  debug = true;

let myurl = "https://www.autorepairehelper.cn/";


function getPicUrl(id){
  var url = myurl +'/file/pic/'+id+'.png';
   return url;
}

// 格式化时间
// util.formatTiem(timeStamp,'Y/M/D h:m:s') 或 util.formatTiem(timeStamp)
function formatTime(date,format = null) {
  var daDate = new Date(date * 1000), // 毫秒级
    formatsArr = ['Y','M','D','h','i','s'],
    returnArr = [],
    year = daDate.getFullYear(),
    month = daDate.getMonth() + 1,
    day = daDate.getDate(),
    hour = daDate.getHours(),
    minute = daDate.getMinutes(),
    second = daDate.getSeconds();
  function formatNumber(n) {
    n = n.toString();
    return n[1] ? n : '0' + n;
  }
  if (format === null) {
    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':');
  }else{
    returnArr.push(year);
    returnArr.push(formatNumber(month));
    returnArr.push(formatNumber(day));
    returnArr.push(formatNumber(hour));
    returnArr.push(formatNumber(minute));
    returnArr.push(formatNumber(second));
    for(let i in returnArr){
      format = format.replace(formatsArr[i], returnArr[i]);
    }
    return format;
  }
}

// AJAX 请求
// util.request(url,type,date)
function request(url, method = 'GET', data = {}) {
  let header = {
    'content-type': 'application/json',
    'X-ECAPI-Sign': '',
    'X-ECAPI-UDID': '',
    'X-ECAPI-UserAgent': 'Platform/Wechat',
    'X-ECAPI-Ver': '1.1.0'
  };

  let token = wx.getStorageSync('token') || '';
  if (token) {
    header['X-ECAPI-Authorization'] = token;
  }

  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      method: method,
      data: data,
      header: header,
      success: function (res) {
    
        // var resData = res.data.data.error_code;
        var error_code = res.data.error_code;
        var statusCode = res.statusCode;
        var Server = res.header.Server;
        if (res.data.error_code === 0 && res.statusCode === 200) {
          console.log(url);
          console.log( JSON.stringify(res));
          resolve(res.data);
        } else {
          reject(res);
        }
      },
      fail: function (err) {
        showToast('网络加载失败','error',600);
      }
    });
  });
}




// 消息提示
// util.showToast(title,type,duration)
function showToast(title, type = 'none', duration = null) {
  let image = '',
    icon = '';
  switch (type) {
  case 'error':
    image = '/images/icon_error.png';
    break;
  case 'success':
    image = '/images/icon_success.png';
    break;
  case 'warning':
    image = '/images/icon_warning.png';
    break;
  case 'none':
    icon = 'none';
    break;
  }
  duration === null ? duration = 1000 : duration;
  wx.showToast({
    title: title,
    icon: icon,
    image: image,
    duration: duration,
    mask: true,
    success: function (res) { },
    fail: function (res) { },
    complete: function (res) { }
  });
}


function saveData(key,info)
{
      
  //     var app = getApp();
  //     app.globalData.userinfo = info;
  // wx.setStorageSync(key, info);
  wx.setStorage({
    //获取数据的key
    key: key,
    data: info,
    success: function (res) {
      // return res.data;
    },
    /**
     * 失败会调用
     */
    fail: function (res) {
      var app = getApp();
      app.globalData.userinfo = info;
    }
  })

}

function getData(key)
{
  
  var info = wx.getStorageSync(key);

  var app = getApp();
  if(!info)
  {
    info = app.globalData.userinfo;
  }
  return info;

  // wx.getStorage({
  //   //获取数据的key
  //   key: key,

  //   success: function (res) {
  //     return res.data;
  //   },
  //   /**
  //    * 失败会调用
  //    */
  //   fail: function (res) {
  //     var app = getApp();
  //     var info = app.globalData.userinfo;
  //     return info;
  //   }
  // })
}


function request2(url, method = 'POST', data = {}) {
  let header = {
    'content-type': 'application/json',
    'X-ECAPI-Sign': '',
    'X-ECAPI-UDID': '',
    'X-ECAPI-UserAgent': 'Platform/Wechat',
    'X-ECAPI-Ver': '1.1.0'
  };

  let token = wx.getStorageSync('token') || '';
  if (token) {
    header['X-ECAPI-Authorization'] = token;
  }

  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      method: method,
      data: data,
      header: header,
      success: function (res) {
        var ret = res.data.code;
        if (parseInt(ret) === 1) {
          console.log(JSON.stringify(res));
          resolve(res.data);
        } else {
          reject(res);
        }
      },
      fail: function (err) {
        reject(err);
        showToast('网络加载失败', 'error', 600);
      }
    });
  });
}
// 模块出口
module.exports = {
  formatTime: formatTime,
  request: request,
  showToast: showToast,
  myurl: myurl,
  getPicUrl: getPicUrl,
  saveData: saveData,
  getData: getData,
  request2: request2,
};
